<?php
/**
 * Created by PhpStorm.
 * User: yuchen
 * Date: 16/4/7
 * Time: 下午8:55
 */

namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Auth;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Input;
use PHPExcel;
use PHPExcel_IOFactory;
use DB;
use Hash;
use Illuminate\Support\Str;
use Cache;
use Illuminate\Support\Facades\File;
use FilesystemIterator;

class CompareController extends Controller
{

    public function compareByPython(Request $request)
    {
        $command = "python fileCompare/diffFilesUbuntu.py fileCompare/se2";
//
//        $command2 = "java -jar fileCompare/tika-app-1.12.jar  --text -i fileCompare/se2 -o fileCompare/output";
//
//        $command3 = "php exec.php";
//        popen($command3,"w");
//        //echo shell_exec($command2);
//
        $path = "fileCompare/se2";
//        $outputPath = "/home/yuchen/Desktop/output";
//        $command = "java -jar fileCompare/tika-app-1.12.jar --text -i '/home/yuchen/Desktop/se2'  -o ~/Desktop/output";

        //shell_exec($command);
        $i=0;
        $file = new FilesystemIterator($path);
        foreach ($file as $fileinfo) {

            $command = "java -jar fileCompare/tika-app-1.12.jar --text "." ".$path."/".$fileinfo->getFilename()." > fileCompare/output/".$fileinfo->getFilename().".txt";
            echo $command."\n";

            shell_exec($command);

            //echo $fileinfo->getFilename() . "\n";
            $i++;
            //   if($fileinfo->isDir()){
            //     $this->test($path . $fileinfo->getFilename() . '/');
            //   }
        }
        echo $i;

        echo shell_exec($command);

    }

    public function testCache(Request $request)
    {
        $expiresAt2 = Carbon::now()->addMinutes(20);
        $testMap = [1,2,3,4,5];
        Cache::add("testXXX",$testMap,$expiresAt2);
//        $testMap = [2,3,4,5];
//        Cache::put("testXX",$testMap,$expiresAt2);
        $inp = $request->input("num");
        $res = Cache::get("testXXX");
        $res[4] = $inp;
        Cache::put("testXXX",$res,$expiresAt2);
        return response()->json(["test"=>$res]);

    }

}